import { AfterViewInit, Component, ElementRef, Input, OnInit, Renderer2 } from '@angular/core';
import { RTable } from '../../r-models/r-table.model';
import { RTableService } from '../../r-service/r-table.service';

@Component({
  selector: 'oy-r-table',
  templateUrl: './r-table.component.html',
  styleUrls: ['./r-table.component.scss']
})

/**
 * 公司：xxx有限公司
 * 版权：xxx2020
 * 描述：基于primeNG的Table组件
 * 作者：oyjl
 * 时间：2020-03-05 10:40:54
 */
export class RTableComponent implements OnInit, AfterViewInit {

  @Input() rTable: RTable;
  private oldPageSize: number; // 保留之前的pagesize
  constructor(
    public rTableService: RTableService,
    public elementRef: ElementRef,
    public renderer: Renderer2) { }

  ngOnInit() {
    window.onresize = () => {
      this.setScrollHeight();
    };
    window.onload = () => {
      this.setScrollHeight();
    };
    this.oldPageSize = this.rTable.paginator.pageSize;
    const t = setTimeout(() => {
      this.setScrollHeight();
      clearTimeout(t);
    }, 100);
  }

  ngAfterViewInit(): void {
    const tableWidth = this.elementRef.nativeElement.querySelector('.tb-top').clientWidth;
    this.renderer.setStyle(this.elementRef.nativeElement.querySelector('.tb-bottom'), 'width', `${tableWidth}px`);
  }

  /**
   * 描述：动态计算滚动高度
   * 作者：oyjl
   * 时间：2022-02-09 10:29:08
   */
  setScrollHeight() {
    if (document.querySelector('.form-main')) {
      const formHeight = document.querySelector('.form-main').clientHeight;
      this.rTable.prop.scrollHeight = (window.innerHeight - 168 - formHeight) + 'px';
    }
  }

  /**
   * 描述：分页表格内部处理
   * 作者：oyjl
   * 时间：2020-03-07 15:40:54
   */
  public onPage(evt) {
    if (this.oldPageSize !== evt.rows) {
      evt.page = 0;
    }
    this.rTable.paginator.pageNo = Number((evt.page + 1) || this.rTable.paginator.pageNo);
    this.rTable.paginator.pageSize = Number(evt.rows || this.rTable.paginator.pageSize);
    this.rTable.paginator.first = (this.rTable.paginator.pageSize * (this.rTable.paginator.pageNo - 1));
    this.rTable.paginator.pageCount = Math.ceil(this.rTable.paginator.total / this.rTable.paginator.pageSize);
    if (this.rTable.paginator.pageNo > this.rTable.paginator.pageCount) {
      this.rTable.paginator.first = 0;
    }
    this.oldPageSize = evt.rows;
    this.rTableService.sendPost(this.rTable, this.rTableService.cacheData.data,
      this.rTableService.cacheData.url, this.rTableService.cacheData.paginatorArgv, false).subscribe(res => {
        if (this.rTableService.afterStatusSwitch(this.rTable.prop.dataConfig, res)) {
          this.rTable.prop.value = this.rTableService.afterDataSwitch(this.rTable.prop.dataConfig, res);
        }
      });
  }

  /**
   * 描述：分页表格内部处理表单页码跳转
   * 作者：oyjl
   * 时间：2020-03-07 15:40:54
   */
  public onPageForm(evt) {
    this.rTable.paginator.pageNo = Number((evt.page + 1) || this.rTable.paginator.pageNo);
    this.rTable.paginator.pageSize = Number(evt.rows || this.rTable.paginator.pageSize);
    this.rTable.paginator.first = (this.rTable.paginator.pageSize * (this.rTable.paginator.pageNo - 1));
    this.rTable.paginator.pageCount = Math.ceil(this.rTable.paginator.total / this.rTable.paginator.pageSize);
    if (this.rTable.paginator.pageNo > this.rTable.paginator.pageCount) {
      this.rTableService.message('error', `输入页码大于总页码数${this.rTable.paginator.pageCount}`);
      return;
    }
    this.rTableService.sendPost(this.rTable, this.rTableService.cacheData.data,
      this.rTableService.cacheData.url, this.rTableService.cacheData.paginatorArgv, false).subscribe(res => {
        if (this.rTableService.afterStatusSwitch(this.rTable.prop.dataConfig, res)) {
          this.rTable.prop.value = this.rTableService.afterDataSwitch(this.rTable.prop.dataConfig, res);
        }
      });
  }

  /**
   * 描述：每一格表格样式判断处理, 返回boolean
   * 作者：oyjl
   * 时间：2020-03-05 10:40:54
   */
  public tbFieldClassHandle(data, col) {
    switch (col.fieldClass && col.fieldClass.symbol) {
      case '>':
        if (data[col.field] > col.fieldClass.value) {
          return true;
        }
        break;
      case '>=':
        if (data[col.field] >= col.fieldClass.value) {
          return true;
        }
        break;
      case '<':
        if (data[col.field] < col.fieldClass.value) {
          return true;
        }
        break;
      case '<=':
        if (data[col.field] <= col.fieldClass.value) {
          return true;
        }
        break;
      case '=':
        if (data[col.field] === col.fieldClass.value) {
          return true;
        }
        break;
      default:
        return false;
    }
  }

  /**
   * 描述：添加数据到下拉菜单中
   * 作者：oyjl
   * 时间：2022-08-17 10:27:02
   */
  addRowData(rowData, options) {
    (options || []).forEach(e => {
      Object.assign(e.queryParams, { rowData });
    });
  }
}
